﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class tipForm : Form
    {
        TipController tip;
        selection parent;
        public tipForm(selection parent)
        {
            tip = new TipController();
            this.parent = parent;
            InitializeComponent();
            initializeContent();
        }

        private void addButton_Click(object sender, EventArgs e)
        {
            addOrEditTip secondForm = new addOrEditTip(this,0);
            secondForm.Text = "Add Tip";
            secondForm.ShowDialog();
        }

        private void editButton_Click(object sender, EventArgs e)
        {
            if (tipTable.Rows.Count > 0)
            {
                addOrEditTip secondForm = new addOrEditTip(this, 1);
                secondForm.Text = "Edit Tip";
                secondForm.ShowDialog();
            }
            else
                MessageBox.Show("No tip to edit.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void backButton_Click(object sender, EventArgs e)
        {
            this.Hide();
            parent.Show();
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            base.OnFormClosing(e);
            DialogResult result = MessageBox.Show("Are you sure you want to exit the program?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (result != DialogResult.Yes)
                e.Cancel = true;
            else
                Environment.Exit(1);
        }

        private void tipForm_Load(object sender, EventArgs e)
        {

        }

        private void deleteButton_Click(object sender, EventArgs e)
        {
            if (tipTable.Rows.Count > 0)
            {
                DialogResult result = MessageBox.Show("Are you sure you want to delete the selected tip?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                if (result != DialogResult.Yes)
                {
                    //do nothing
                }
                else
                {
                    DateTime date = DateTime.Parse(tipTable.SelectedRows[0].Cells[0].Value.ToString());
                    bool success = new TipController().deleteTip(date.ToString("yyyy-MM-dd"));
                    if (success)
                    {
                        initializeContent();
                    }
                }
            }
            else
            {
                MessageBox.Show("No tip to delete.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void initializeContent()
        {
            BindingSource bindingSource = new BindingSource();
            bindingSource.DataSource = tip.getData();
            tipTable.DataSource = bindingSource;

        }

        public String getSelectedDate()
        {
            String[] date = tipTable.SelectedRows[0].Cells[0].Value.ToString().Split(' ');
            return date[0];
        }

        public decimal getAmount()
        {
            return Decimal.Parse(tipTable.SelectedRows[0].Cells[1].Value.ToString());
        }
    }
}
